package com.yunfei.procedure;
import com.yunfei.api.financialCalculator.param.RiQiCalParam;
import com.yunfei.api.financialCalculator.param.YangLaoCalParam;
import com.yunfei.api.financialCalculator.result.RiQiCalResult;
import com.yunfei.api.financialCalculator.result.YangLaoCalResult;
import com.yunfei.utils.jdbc.AiiidbUtil;
import oracle.jdbc.OracleTypes;
import java.sql.*;
/**
 * 调用接口：
 *  CPT_FUNC_PKG.SP_YLJSQ
 *                     (BIRTH_YEAR INTEGER, --出生年份
 *                      RETIRE_AGE INTEGER,  --退休年龄
 *                      WANT_GET_AMT NUMBER, --想要获得的钱
 *                      NEED_GET_AMT OUT NUMBER, --输出参数：现在每个月需要攒的钱
 *                      PO_SP_RSLT_FG OUT INTEGER, --当这个值为0时，代表计算错误，前台按 PO_SP_RSLT_INFO 的值提示
 *                      PO_SP_RSLT_INFO OUT VARCHAR2
 *                      )
* @Description: 养老计算器
* @Author: zhengyunfei
* @Date: 2019/2/15
* @time:16:44
*/
public class YangLaoCalProcedure {
    private Connection conn;
    private CallableStatement stat;
    private ResultSet rs;
    String calSql = "call jcpt_func_pkg.sp_yljsq(?,?,?,?,?,?)";
    /**
    * @Description: 养老计算器存储过程
    * @Author: zhengyunfei
    * @Date: 2018/4/24
    * @time:13:49
    */
    public YangLaoCalResult callProcedure(AiiidbUtil aiiidbUtil, YangLaoCalParam param) {
        YangLaoCalResult result=new YangLaoCalResult();
        try {
            String url=aiiidbUtil.getJdbcUrl();
            String driverName=aiiidbUtil.getDriveClassName();
            String username=aiiidbUtil.getJdbcUserName();
            String password=aiiidbUtil.getJdbcPassword();
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.prepareCall(calSql);
            // 2个输入参数和2个输出参数
            stat.setString(1, param.getBirthYearInteger());
            stat.setString(2, param.getRetireAgeInteger());
            stat.setString(3, param.getWantGetAmtNumber());
            stat.registerOutParameter(4, OracleTypes.VARCHAR);
            stat.registerOutParameter(5, OracleTypes.VARCHAR);
            stat.registerOutParameter(6, OracleTypes.VARCHAR);
            stat.execute();
            //返回结果
            String needGetAmt= stat.getString(4);
            String poSpRsltFg= stat.getString(5);
            String poSpRsliInfo= stat.getString(6);
            result.setNeedGetAmt(needGetAmt);
            result.setPoSpRsliFg(poSpRsltFg);
            result.setPoSpRsliInfo(poSpRsliInfo);
            System.out.println("养老计算器返回结果: " + result.getNeedGetAmt()+","+","+result.getPoSpRsliInfo());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, stat, rs);
        }
        return result;
    }

    // 关闭连接
    public void close(Connection conn, CallableStatement stat, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stat = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void main(String[] args) {
       // new GetTouZiZheRenZhengInfoProcedure().callProcedure("20180417132146779447466");
    }
}